const { defineConfig } = require('@vue/cli-service')

let target = process.env.npm_lifecycle_event
const isLib = target === 'build:dogeo-map'
const isProd = process.env.NODE_ENV === 'production'
const isSingle = process.env.NODE_ENV === 'single'
const packageName = 'dmapt'

module.exports = defineConfig({
  pages: {
    index: {
      entry: isProd ? 'src/loader.js' : 'src/main.js',
      template: 'public/index.html',
      filename: 'index.html',
      inject: true
    },
    main: {
      entry: 'src/main.js',
      template: 'public/index.html',
      filename: 'main.html',
      inject: true
    },
    'micro-app': {
      entry: 'src/micro-app.js',
      template: 'public/index.html',
      filename: 'micro-app.html',
      inject: true
    }
  },
  transpileDependencies: true,
  publicPath: isSingle ? './' : isProd ? '/df3s/' + packageName + '/app/' : '/',
  assetsDir: 'static',
  productionSourceMap: false,
  outputDir: isSingle ? '../dest/app' : !isLib ? '../dist/app' : '../dist/dogeo-map',
  filenameHashing: false, // 取消hash
  configureWebpack: (config) => {
    return {
      output: {
        libraryTarget: 'umd',
        library: packageName,
        // filename: `[name].${version}.${timestamp}.js`,
        // chunkFilename: `[name].${version}.${timestamp}.js`
      },
      // externals: isLib ? [
      //   {
      //     'vue': 'vue',
      //     'vuex': 'vuex',
      //     'element-ui': 'element-ui',
      //     'cesium': 'cesium',
      //     'cesium/Source/Cesium.js': 'cesium',
      //     'geotiff': 'geotiff'
      //   }
      // ] : {},
      externals: isProd ? [
        {
          'vue': 'vue',
          'vuex': 'vuex',
          'vue-router': 'vue-router',
          'system': 'system',
          'axios': 'axios',
          'iview': 'iview',
          'moment': 'moment',
          'echarts': 'echarts',
          'element-ui': 'element-ui',
          'less': 'less',
          'cesium': 'cesium',
          'cesium/Source/Cesium.js': 'cesium',
          'geotiff': 'geotiff'
        },
        !isLib ? /^@daelui\/dogjs/ : /^@daelui/,
        !isLib ? function(context, request, callback) {
          if (/vendor\/monaco-editor$|vendor\/(vue-)?quill|vendor\/highlightjs/.test(request)) {
            return callback(null, request.replace(/.*vendor\//, ''))
          }
          callback()
        }: '',
        function(context, request, callback) {
          if (/^@daelui\/dogui\/dist|element-ui\/lib\/theme/.test(request)) {
            return callback(null, request)
          }
          callback()
        }
      ] : {}
    }
  }
})
